﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;

namespace Testify
{
    public partial class ManualGrading : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool successful = true;

            OracleCommand cmdSave = new OracleCommand(@"
BEGIN responses.gradeResponse(
  p_StudentID      => :p_StudentID,
  p_QuestionID     => :p_QuestionID,
  p_ResponseCredit => :p_ResponseCredit);
END;".Replace("\n", Environment.NewLine),
                new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString));
            cmdSave.Parameters.Add("p_StudentID",      DBNull.Value);
            cmdSave.Parameters.Add("p_QuestionID",     DBNull.Value);
            cmdSave.Parameters.Add("p_ResponseCredit", DBNull.Value);

            cmdSave.Connection.Open();

            for (int i = 0; i < grdUngradedResponses.Rows.Count; i++)
            {
                string credit = ((TextBox)grdUngradedResponses.Rows[i].FindControl("txtCredit")).Text;

                if (!String.IsNullOrWhiteSpace(credit))
                {
                    cmdSave.Parameters["p_StudentID"].Value      = Convert.ToString(grdUngradedResponses.DataKeys[i]["student_id"]);
                    cmdSave.Parameters["p_QuestionID"].Value     = Convert.ToString(grdUngradedResponses.DataKeys[i]["question_id"]);
                    cmdSave.Parameters["p_ResponseCredit"].Value = credit;

                    try
                    {
                        cmdSave.ExecuteNonQuery();
                    }
                    catch (OracleException oe)
                    {
                        Label lblError = (Label)grdUngradedResponses.Rows[i].FindControl("lblError");

                        successful = false;

                        switch (oe.Code.ToString())
                        {
                            case "20001":
                                lblError.Text    = "Credit applied must be between 0 and the question's [weight].";
                                lblError.Visible = true;
                                break;
                            default:
                                lblError.Text    = String.Empty;
                                lblError.Visible = false;
                                break;
                        }
                    }

                    cmdSave.Parameters["p_StudentID"].Value      = DBNull.Value;
                    cmdSave.Parameters["p_QuestionID"].Value     = DBNull.Value;
                    cmdSave.Parameters["p_ResponseCredit"].Value = DBNull.Value;
                }
            }

            cmdSave.Connection.Close();

            if (successful)
                grdUngradedResponses.DataBind();
        }
    }
}